package com.example.sugar_api.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.sugar_api.pojo.ProvinceInfo;
import com.example.sugar_api.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author NanHuang
 * @Date 2023/1/27
 */
@RestController
@RequestMapping("/trade")
public class TradeController {
    @Autowired
    private TradeService tradeService;

    @PostMapping("/province-count")
    public JSONObject provinceCount(@RequestParam(value = "dt",defaultValue = "20230126")String dt){
        // 1 查询结果
        List<ProvinceInfo> pi = tradeService.countByProvince(dt);
        // 2 转为固定格式JSON
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");
        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();
        // mapData
        for (ProvinceInfo pc : pi) {
            JSONObject j = new JSONObject();
            j.put("name",pc.getProvinceName());
            j.put("value",pc.getOrderAmount());
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(pc.getOrderCount());
            j.put("tooltipValues",tooltipValues);
            j.put("url","www.danan.life");
            mapData.add(j);
        }
        data.put("valueName","销售额");
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("下单数量");
        JSONArray tooltipUnits = new JSONArray();
        tooltipNames.add("单");
        data.put("tooltipUnits",tooltipUnits);
        data.put("tooltipNames",tooltipNames);
        data.put("mapData",mapData);
        result.put("data",data);

        return result;
    }
}
